#include "collisia_control.h"

extern int G_W,G_H;
extern HUD *global_hud;
extern QVector<int> *vect_exp_count;

COLLISIA_CONTROL::COLLISIA_CONTROL(SCENE *scene, QGraphicsObject *collisia_item, QObject *father, QTimeLine *global_tl, CASH *cash, int loop_timer):QObject(father)
{

    qsrand(QTime::currentTime().second());

    bum_1 = new SOUND_POWER(cash,"Res/Sound/Explosion/1.ogg",this);
    bum_2 = new SOUND_POWER(cash,"Res/Sound/Explosion/2.ogg",this);
    bum_3 = new SOUND_POWER(cash,"Res/Sound/Explosion/3.ogg",this);

    this->cash=cash;

    this->global_tl=global_tl;

    this->scene=scene;

    this->collisia_item=collisia_item;

    col_vo=collisia_item->shape();                                     // Cчитали шейп обьекта с которым работаем

    timer_id=startTimer(loop_timer);

}

void COLLISIA_CONTROL::timerEvent(QTimerEvent *)
{

    QList<QGraphicsItem *> now = collisia_item->collidingItems();

    if(now.count()>1){
        int max=0;
        for(int i=0;i<now.count();i++)
            if(now[i]->type()>max) max=now[i]->type();

        emit Collisia( collisia_item->pos() - QPointF(50, 50) , max );
        global_hud->add_score(max);

//        if(collisia_item->objectName() == "Shuttle_Bullet")
//            for(int i=0;i<now.count();i++)
//                if(now[i]->data(777).toString() == "Shuttle"){ qDebug()<<"aga";  break;}

        switch(1+qrand()%(3-1+1)){

            case 1: bum_1->Play_Now();
            break;
            case 2: bum_2->Play_Now();
            break;
            case 3: bum_3->Play_Now();
            break;
            default: qDebug()<<"Switch sound bum collisia error";

        }



        SPRITE_EXPLOSION *mng = new SPRITE_EXPLOSION(collisia_item->pos(),cash, qrand()%(11));

        scene->addItem(mng);
        mng->setZValue(0.6);
        mng->setPos( collisia_item->pos() - QPointF((mng->boundingRect().width())/2, mng->boundingRect().height()/2) );

        connect(global_tl, SIGNAL(valueChanged(qreal)), mng, SLOT(slot_run()));

    }

}

 COLLISIA_CONTROL::~COLLISIA_CONTROL()
{

    killTimer(timer_id);

}

